package rabbitmq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author lumi
 * @date 2019/12/25 21:59
 */
public class Producer01 {
    private static final String QUEUE  = "helloworld";
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;


        try {
            connection = connectionFactory.newConnection();
            Channel channel = connection.createChannel();
             channel.queueDeclare(QUEUE,true,false,false,null);

             String msg = "hello world";
             //发送消息
            channel.basicPublish("",QUEUE,null,msg.getBytes());
            System.out.println("send to mq msg"+ msg);

        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
